/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package domain;

import java.util.Random;

/**
 *
 * @author "Jeroen"
 */
public class MineField {

    private int width;
    private int height;
    private int bombs;
    private Square[][] field;

    /**public MineField(int size, int bombs) {
        this(size, size, bombs);
    }**/

    public MineField(int width, int height, int bombs) {
        this.width = width;
        this.height = height;
        this.bombs = bombs;
        field = new Square[width][height];

        for (int i = 0; i < width; i++) {
            for (int j = 0; j < height; j++) {
                field[i][j] = new Square();
            }
        }
        generateField();
    }

    public void setWidth(int width) {
        if (width < 9) {
            throw new IllegalArgumentException();
        } else {
            this.width = width;
        }
    }

    public void setHeight(int height) {
        if (height < 9) {
            throw new IllegalArgumentException();
        } else {
            this.height = height;
        }
    }

    public int getWidth() {
        return width;
    }

    public int getHeight() {
        return height;
    }

    //plaatst bommen en berekent aantal
    private void generateField() {
        Random r = new Random();
        for (int i = 0; i < bombs; i++) {
            int x = r.nextInt(width - 1);
            int y = r.nextInt(height - 1);
            while (field[x][y].isBomb()) {
                x = r.nextInt(width);
                y = r.nextInt(height);
            }
            field[x][y].setBomb(true);
        }
        for (int i = 0; i < width; i++) {
            for (int j = 0; j < height; j++) {
                countBombs(i, j);
            }
        }
    }

    private void placeBombs(int x, int y) {
        Random r = new Random();
        for (int n = 0; n < bombs; n++) {
            int xx = r.nextInt(width - 1);
            int yy = r.nextInt(height - 1);
            while (field[xx][yy].isBomb() && !(xx >= x - 1 && xx <= x + 1 && yy >= y - 1 && yy <= y + 1)) {
                xx = r.nextInt(width);
                yy = r.nextInt(height);
            }
            field[xx][yy].setBomb(true);
            for (int i = -1; i < 2; i++) {
                for (int j = -1; j < 2; j++) {
                    if (i != 0 && y != 0) {
                        field[xx + i][yy + j].addNearby_bomb();
                    }
                }
            }
        }
    }

    //telt het aantal bommen in de rondomliggende velden
    private void countBombs(int i, int j) {
        if (!field[i][j].isBomb()) {
            int counter = 0;
            if (i > 0) {
                int x = i - 1;
                int y = j;
                if (j > 0) {
                    y--;
                    if (field[x][y].isBomb()) {
                        counter++;
                    }
                    y++;
                }
                if (field[x][y].isBomb()) {
                    counter++;
                }
                if (j < height - 1) {
                    y++;
                    if (field[x][y].isBomb()) {
                        counter++;
                    }
                }
            }

            int y = j;
            if (j > 0) {
                y--;
                if (field[i][y].isBomb()) {
                    counter++;
                }
                y++;
            }
            if (j < height - 1) {
                y++;
                if (field[i][y].isBomb()) {
                    counter++;
                }
            }

            if (i < width - 1) {
                int x = i + 1;
                y = j;
                if (j > 0) {
                    y--;
                    if (field[x][y].isBomb()) {
                        counter++;
                    }
                    y++;
                }
                if (field[x][y].isBomb()) {
                    counter++;
                }
                if (j < height - 1) {
                    y++;
                    if (field[x][y].isBomb()) {
                        counter++;
                    }
                }
            }
            field[i][j].setNearby_bombs(counter);
        }
    }

    //geeft het ganse speelveld: bommen voorgesteld door * of aantal rondomliggende bommen
    /**public String[][] getField() {
        String[][] veld = new String[width][height];
        for (int i = 0; i < width; i++) {
            for (int j = 0; j < height; j++) {
                if (field[i][j].isBomb()) {
                    veld[i][j] = "*";
                } else {
                    veld[i][j] = ((Integer) field[i][j].getNearby_bombs()).toString();
                }
            }
        }
        return veld;
    }**/
    
    public char[][] getViewModel()
    {
        char[][] veld = new char[width][height];
        for (int i = 0; i < width; i++) {
            for (int j = 0; j < height; j++) {
                veld[i][j] = field[i][j].toChar();
            }
        }
        return veld;
    }

    public Square getSquare(int x, int y)
    {
        return field[x][y];
    }
}
